<template>
	<a-drawer
		title="联系人选择"
		:width="1100"
		:visible="visible"
		:destroy-on-close="true"
		placement="right"
		:body-style="{ paddingBottom: '80px', 'padding-top': '0px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
		:scroll-y="false"
	>
		<a-card :bordered="false">
			<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState"
					class="ant-advanced-search-form mb-4">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="关键字" name="searchKey">
							<a-input v-model:value.trim="searchFormState.searchKey" placeholder="请输入用户名/手机号"
									 allowClear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="身份" name="identify">
							<a-select v-model:value="searchFormState.identify" placeholder="请选择身份"
									  :options="identifyOptions" allowClear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-button type="primary" @click="table.refresh(true)">查询</a-button>
						<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
					</a-col>
				</a-row>
			</a-form>
			<s-table
				ref="table"
				:columns="columns"
				:data="loadData"
				:alert="options.alert.show"
				bordered
				:row-key="(record) => record.id"
				:tool-config="toolConfig"
				:row-selection="options.rowSelection"
				:sticky="true"
				:scroll="scrollHeight"
			>
				<template #operator class="table-operator">
					<a-space>
						<a-button type="primary" @click="openForm">
							<template #icon>
								<plus-outlined/>
							</template>
							新增
						</a-button>
						<!--						<a-button danger @click="deleteBatchBizCabinetLinkman()">删除</a-button>-->
					</a-space>
				</template>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'identify'">
						{{ $TOOL.dictTypeData('LINKMAN_IDENTIFY', record.identify) }}
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-space>
							<a @click="formRef.onOpen(cabinetGroupId, cabinetInfoId, record)">编辑</a>
							<a-divider type="vertical"/>
							<a-popconfirm title="确定要删除吗？" @confirm="deleteBizCabinetLinkman(record)">
								<a-button type="link" danger size="small">删除</a-button>
							</a-popconfirm>
						</a-space>
					</template>
				</template>
			</s-table>
		</a-card>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
		</template>
	</a-drawer>
	<Form ref="formRef" @successful="table.refresh(true)"/>
</template>

<script setup name="cabinetlinkman">
	import {message} from 'ant-design-vue'
	import tool from '@/utils/tool'
	import Form from './form.vue'
	import bizCabinetLinkmanApi from '@/api/biz/bizCabinetLinkmanApi'

	const scrollHeight = ref()
	let searchFormState = reactive({})
	// 弹窗是否打开
	let visible = $ref(false)
	let cabinetGroupId = $ref()
	let cabinetInfoId = $ref()
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const emit = defineEmits({successful: null})
	const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
	const columns = [
		{
			title: '学工号',
			dataIndex: 'jobNumber'
		},
		{
			title: '姓名',
			dataIndex: 'userName'
		},
		{
			title: '组织',
			dataIndex: 'orgName'
		},
		{
			title: '身份',
			dataIndex: 'identify'
		},
		{
			title: '手机号',
			dataIndex: 'mobile'
		},
		{
			title: '联系电话',
			dataIndex: 'telephone'
		},
		{
			title: '办公地点',
			dataIndex: 'officeAddress'
		}
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	})
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const onOpen = (_cabinetGroupId, _cabinetInfoId) => {
		scrollHeight.value = {y: (window.innerHeight - 380)};
		console.log('cabinetGroupId=', _cabinetGroupId, '& cabinetInfoId=', _cabinetInfoId)
		cabinetGroupId = _cabinetGroupId
		cabinetInfoId = _cabinetInfoId
		visible = true
	}
	// 关闭抽屉
	const onClose = () => {
		visible = false
		cabinetGroupId = ref()
		cabinetInfoId = ref()
		emit('successful')
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		searchFormParam.cabinetGroupId = cabinetGroupId
		searchFormParam.cabinetInfoId = cabinetInfoId
		return bizCabinetLinkmanApi.bizCabinetLinkmanPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 删除
	const deleteBizCabinetLinkman = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizCabinetLinkmanApi.bizCabinetLinkmanDelete(params).then(() => {
			message.success('删除成功')
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizCabinetLinkman = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		bizCabinetLinkmanApi.bizCabinetLinkmanDelete(params).then(() => {
			message.success('删除成功')
			table.value.clearRefreshSelected()
		})
	}
	const openForm = () => {
		formRef.value.onOpen(cabinetGroupId, cabinetInfoId, null)
	}
	const identifyOptions = tool.dictList('LINKMAN_IDENTIFY')
	defineExpose({
		onOpen
	})
</script>

<style lang="less" scoped>
	/deep/ .ant-table-body {
		overflow-x: hidden;

	&
	::-webkit-scrollbar {
		/*滚动条整体样式*/
		width: 5px;
		height: 1px;
	}

	&
	::-webkit-scrollbar-thumb {
		/*滚动条里面的滑块*/
		border-radius: 5px;
		background: rgba(100, 177, 240, 0.3);
	}

	&
	::-webkit-scrollbar-track {
		/*滚动条里面轨道背景*/
		border-radius: 5px;
		background: rgba(0, 0, 0, 0.1);
	}

	}
</style>
